<template>
  <div class="user-favorite">
    <el-card>
      <template #header>
        <div class="card-header">
          <h3>我的收藏</h3>
          <el-radio-group v-model="typeFilter" size="small">
            <el-radio-button label="all">全部</el-radio-button>
            <el-radio-button label="course">课程</el-radio-button>
            <el-radio-button label="article">文章</el-radio-button>
            <el-radio-button label="teacher">讲师</el-radio-button>
          </el-radio-group>
        </div>
      </template>

      <!-- 课程收藏 -->
      <div v-if="typeFilter === 'all' || typeFilter === 'course'" class="favorite-section">
        <h4>课程收藏</h4>
        <el-row :gutter="20">
          <el-col
            v-for="course in filteredCourses"
            :key="course.id"
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
          >
            <el-card
              class="favorite-card"
              shadow="hover"
              @click="viewCourse(course)"
            >
              <el-image
                :src="course.cover"
                class="card-cover"
                fit="cover"
              />
              <div class="card-content">
                <h5>{{ course.title }}</h5>
                <p>{{ course.description }}</p>
                <div class="card-meta">
                  <span>
                    <el-icon><User /></el-icon>
                    {{ course.teacher }}
                  </span>
                  <span>
                    <el-icon><Star /></el-icon>
                    {{ course.rating }}
                  </span>
                </div>
                <div class="card-footer">
                  <span class="price">¥{{ course.price }}</span>
                  <el-button
                    type="danger"
                    link
                    @click.stop="removeFavorite(course)"
                  >
                    取消收藏
                  </el-button>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 文章收藏 -->
      <div v-if="typeFilter === 'all' || typeFilter === 'article'" class="favorite-section">
        <h4>文章收藏</h4>
        <el-table :data="filteredArticles" style="width: 100%">
          <el-table-column label="文章信息" min-width="400">
            <template #default="{ row }">
              <div class="article-info">
                <el-image
                  :src="row.cover"
                  class="article-cover"
                  fit="cover"
                />
                <div class="article-detail">
                  <h5>{{ row.title }}</h5>
                  <p>{{ row.description }}</p>
                  <div class="article-meta">
                    <span>
                      <el-icon><User /></el-icon>
                      {{ row.author }}
                    </span>
                    <span>
                      <el-icon><View /></el-icon>
                      {{ row.views }}
                    </span>
                    <span>
                      <el-icon><ChatDotRound /></el-icon>
                      {{ row.comments }}
                    </span>
                  </div>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="收藏时间" width="180">
            <template #default="{ row }">
              {{ row.favoriteTime }}
            </template>
          </el-table-column>

          <el-table-column label="操作" width="120">
            <template #default="{ row }">
              <el-button
                type="primary"
                link
                @click="viewArticle(row)"
              >
                查看
              </el-button>
              <el-button
                type="danger"
                link
                @click="removeFavorite(row)"
              >
                取消收藏
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 讲师收藏 -->
      <div v-if="typeFilter === 'all' || typeFilter === 'teacher'" class="favorite-section">
        <h4>讲师收藏</h4>
        <el-row :gutter="20">
          <el-col
            v-for="teacher in filteredTeachers"
            :key="teacher.id"
            :xs="24"
            :sm="12"
            :md="8"
            :lg="6"
          >
            <el-card
              class="favorite-card teacher-card"
              shadow="hover"
              @click="viewTeacher(teacher)"
            >
              <div class="teacher-avatar">
                <el-avatar :size="80" :src="teacher.avatar" />
                <div class="teacher-info">
                  <h5>{{ teacher.name }}</h5>
                  <p>{{ teacher.title }}</p>
                </div>
              </div>
              <div class="teacher-stats">
                <div class="stat-item">
                  <div class="stat-value">{{ teacher.courseCount }}</div>
                  <div class="stat-label">课程</div>
                </div>
                <div class="stat-item">
                  <div class="stat-value">{{ teacher.studentCount }}</div>
                  <div class="stat-label">学生</div>
                </div>
                <div class="stat-item">
                  <div class="stat-value">{{ teacher.rating }}</div>
                  <div class="stat-label">评分</div>
                </div>
              </div>
              <div class="card-footer">
                <el-button
                  type="danger"
                  link
                  @click.stop="removeFavorite(teacher)"
                >
                  取消收藏
                </el-button>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import {
  User,
  Star,
  View,
  ChatDotRound
} from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const router = useRouter()

// 类型筛选
const typeFilter = ref('all')

// 分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 课程数据
const courses = ref([
  {
    id: 1,
    title: 'Vue.js 3.0 实战教程',
    description: '从入门到精通的 Vue.js 3.0 课程',
    cover: 'https://picsum.photos/200/200?random=1',
    teacher: '张老师',
    rating: 4.8,
    price: 299,
    type: 'course'
  },
  {
    id: 2,
    title: 'React 18 新特性详解',
    description: '深入理解 React 18 的新特性和最佳实践',
    cover: 'https://picsum.photos/200/200?random=2',
    teacher: '李老师',
    rating: 4.9,
    price: 399,
    type: 'course'
  }
])

// 文章数据
const articles = ref([
  {
    id: 1,
    title: 'Vue 3 组合式 API 最佳实践',
    description: '深入探讨 Vue 3 组合式 API 的使用技巧和最佳实践',
    cover: 'https://picsum.photos/200/200?random=3',
    author: '张老师',
    views: 1234,
    comments: 56,
    favoriteTime: '2024-03-15 14:30',
    type: 'article'
  }
])

// 讲师数据
const teachers = ref([
  {
    id: 1,
    name: '张老师',
    title: '高级前端工程师',
    avatar: 'https://picsum.photos/200/200?random=4',
    courseCount: 12,
    studentCount: 1234,
    rating: 4.9,
    type: 'teacher'
  }
])

// 根据类型筛选数据
const filteredCourses = computed(() => {
  return courses.value
})

const filteredArticles = computed(() => {
  return articles.value
})

const filteredTeachers = computed(() => {
  return teachers.value
})

// 查看课程
const viewCourse = (course) => {
  router.push(`/course/${course.id}`)
}

// 查看文章
const viewArticle = (article) => {
  router.push(`/article/${article.id}`)
}

// 查看讲师
const viewTeacher = (teacher) => {
  router.push(`/teacher/${teacher.id}`)
}

// 取消收藏
const removeFavorite = (item) => {
  ElMessageBox.confirm(
    '确定要取消收藏吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // TODO: 调用取消收藏 API
    ElMessage.success('已取消收藏')
  }).catch(() => {})
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  // TODO: 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // TODO: 重新加载数据
}
</script>

<style scoped>
.user-favorite {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  font-size: 18px;
}

.favorite-section {
  margin-bottom: 30px;
}

.favorite-section h4 {
  margin: 0 0 20px;
  font-size: 16px;
  color: var(--el-text-color-regular);
}

.favorite-card {
  margin-bottom: 20px;
  cursor: pointer;
  transition: transform 0.3s;
}

.favorite-card:hover {
  transform: translateY(-5px);
}

.card-cover {
  width: 100%;
  height: 160px;
  border-radius: 4px 4px 0 0;
}

.card-content {
  padding: 15px;
}

.card-content h5 {
  margin: 0 0 10px;
  font-size: 16px;
  line-height: 1.4;
}

.card-content p {
  margin: 0 0 10px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  line-height: 1.4;
  height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.card-meta {
  display: flex;
  gap: 16px;
  color: var(--el-text-color-secondary);
  font-size: 13px;
  margin-bottom: 10px;
}

.card-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  color: var(--el-color-danger);
  font-size: 18px;
  font-weight: bold;
}

.article-info {
  display: flex;
  gap: 16px;
}

.article-cover {
  width: 120px;
  height: 80px;
  border-radius: 4px;
}

.article-detail {
  flex: 1;
}

.article-detail h5 {
  margin: 0 0 8px;
  font-size: 16px;
}

.article-detail p {
  margin: 0 0 8px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
}

.article-meta {
  display: flex;
  gap: 16px;
  color: var(--el-text-color-secondary);
  font-size: 13px;
}

.article-meta span {
  display: flex;
  align-items: center;
  gap: 4px;
}

.teacher-card {
  text-align: center;
}

.teacher-avatar {
  padding: 20px 0;
}

.teacher-info {
  margin-top: 10px;
}

.teacher-info h5 {
  margin: 0 0 5px;
  font-size: 16px;
}

.teacher-info p {
  margin: 0;
  color: var(--el-text-color-secondary);
  font-size: 14px;
}

.teacher-stats {
  display: flex;
  justify-content: space-around;
  padding: 15px 0;
  border-top: 1px solid var(--el-border-color-lighter);
  border-bottom: 1px solid var(--el-border-color-lighter);
}

.stat-item {
  text-align: center;
}

.stat-value {
  font-size: 18px;
  font-weight: bold;
  color: var(--el-color-primary);
}

.stat-label {
  font-size: 12px;
  color: var(--el-text-color-secondary);
  margin-top: 4px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 